﻿@{
    ViewBag.Title = "采集网络文章";
}

@section styles{
    <style>
        .alter {
            height: 5px;
        }
    </style>
}
<div ms-controller="collection">
    <div class="am-cf am-padding">
        <div class="am-fl am-cf">
            <strong class="am-text-primary am-text-lg">文章采集</strong> /
            <small>collection</small>
        </div>
    </div>
    <form action="/Collection/PrevContent" id="ffff" method="post" target="_blank">

        <div class="am-g">
            <div class="am-u-sm-12 am-u-md-12">
                <input type="text" placeholder="标题网址" class="am-form-field" 
                       name="url"
                       ms-duplex="collection_url" />
            </div>
        </div>
        <p class="alter">
            {{title}}
        </p>
        <div class="am-g">
            <div class="am-u-sm-12 am-u-md-11">
                <input type="text" placeholder="标题dom元素" class="am-form-field" ms-duplex="title_dom" />
            </div>
            <div class="am-u-sm-12 am-u-md-1">
                <button type="button" class="am-btn am-btn-warning" ms-click="test_get_title" data-ajax>测试</button>
            </div>
        </div>
        <p class="alter">
            &nbsp;
        </p>
        <div class="am-g">
            <div class="am-u-sm-12 am-u-md-5">
                <input type="text" placeholder="详细内容dom元素，例如：//div[(at符)class='Post-body']"
                       name="detaildom"
                       class="am-form-field" ms-duplex="content_dom" />
            </div>
            <div class="am-u-sm-12 am-u-md-5">
                <input type="text" placeholder="连续采集页数 默认是25" class="am-form-field" 
                       name=""
                       ms-duplex="pages" />
                <input type="hidden" name="IsGetDetail" value="true" />
            </div>
            <div class="am-u-sm-12 am-u-md-2">
                <button type="button" class="am-btn am-btn-warning" 
                        ms-click="test_get_detail">测试</button>
            </div>
        </div>
    </form>

    <p class="alter">
    </p>
    <div class="am-g">
        <div class="am-u-sm-12 am-u-md-1">
            <button type="button" class="am-btn am-btn-warning" ms-click="save_post" data-ajax>采集</button>
        </div>
        <p class="alter" ms-if="last_post_id">
            文章编号：{{last_post_id}} <a ms-attr-href="/post/edit/{{last_post_id}}" target="_blank">编辑</a>
        </p>
    </div>
</div>

@section scripts{
    <script>
        var vm = avalon.define({
            $id: 'collection',
            collection_url: '',
            title_dom: '',
            title: '',
            test_get_title: function () {
                $.post('title', { url: vm.collection_url, titleDom: vm.title_dom }).success(function (data) {
                    vm.title = data.title;
                }).error(function (data) {
                    alert(data);
                });
            },
            content_dom: '',
            pages: '',
            detail: '',
            last_post_id: '',
            test_get_detail: function () {
                if (vm.collection_url && vm.content_dom) {
                    $('#ffff').submit();
                } else {
                    alert('网址和dom必填')
                }
            },
            save_post: function () {
                $.post('SavePost', {
                    url: vm.collection_url,
                    titleDom: vm.title_dom,
                    detailDom: vm.content_dom,
                    detail: vm.detail,
                    pages: vm.pages
                }).success(function (id) {
                    vm.last_post_id = id;
                }).error(function () {
                    error_msg('error');
                });
            }
        });

    </script>
}